<?php
namespace Common;

class Page {

	private $m_metaTags = array();
	private $m_charset;

	/**
	 * Called when using new PageView();
	 * @param string $charset
	 **/
	public function __construct($charset = "utf-8") {
		$this -> m_charset = $charset;
	}

	/**
	 * Adds a CSS stylesheet to the head of the document
	 * @param urlstring $href url to css file
	 **/
	public function AddStyleSheet($href) {
		//TODO: "/>" is not really valid for "HTML 4.01 Transitional" so these tags are not ended
		$this -> m_metaTags[] = "<link rel='StyleSheet' href='$href' type='text/css'";
	}

	/**
	 * Builds meta and CSS tags as a HTML/XML string
	 *
	 * @param bool $isXML is the document an XML file and tags should be closed
	 * @return string
	 */
	private function BuildHeadTags($isXML) {
		$end = ">";
		if ($isXML) {
			$end = "/>";
		}
		$retValue = "";
		foreach ($this->m_metaTags as $tag) {
			$retValue .= $tag . "$end\n            ";
			// "\n            " for readability
		}
		return $retValue;
	}

	/**
	 * Returns a HTML 4.01 Transitional page
	 * @param string $title
	 * @param string $body
	 * @return string
	 **/
	public function GetHTMLPage($title, $body, $loginform) {

		$head = $this -> BuildHeadTags(true);

		$register = "";
		$login = "";
		$database = new \Model\Database();
		$database -> Connect(new \Model\DBConfig());
		$lh = new \Model\LoginHandler($database);
		$lv = new \View\LoginView();
		$postview = new \View\PostView();
		$postarray = new \Model\PostArray();
		$postCatalog = new \Model\PostHandler($database);
		$posts = $postCatalog -> getAllPosts();
		$archive = $postview -> showListOfPosts($posts, false);
		$meta = "";
		
		if ($lh->IsLoggedIn() == true) {
			
			$meta .= "<h2>META</h2>
						<ul>
							<li><a href='index.php?page=newpost'>Ny post</a></li>
						</ul>";
		}
		
		$xhtml = "
        <!DOCTYPE HTML SYSTEM>
        <html>
          <head>
            <title>$title</title>
            <meta http-equiv='content-type' content='text/html; charset=$this->m_charset'>
            <meta name='apple-mobile-web-app-status-bar-style' content='white' />
            <!--5grid--><script src=\"css/5grid/viewport.js\"></script><!--[if lt IE 9]><script src=\"css/5grid/ie.js\"></script><![endif]--><link rel=\"stylesheet\" href=\"css/5grid/core.css\" />
            <!--[if IE 9]><link rel=\"stylesheet\" href=\"css/style-ie9.css\" /><![endif]-->
            <link rel='icon'type='image/png' href='Css/images/favicon.ico'>
            <link rel='apple-touch-icon' href='Css/images/iPhoneIcon_Big.png'/>
            <link rel='apple-touch-startup-image' href='Css/images/startup.png'>
            <script type='text/javascript' src='ckeditor/ckeditor.js'></script>
			<script type='text/javascript' src='ckeditor/adapters/jquery.js'></script>
            $head
          </head>
          <body>
	          <div id='wrapper'>
				<div id='header-wrapper'>
					<div id='header'>
						<div id='logo'>
							<h1><a href='index.php'>edport.se </a></h1>
							<p>Projekt för PHP</p>
						</div>
				<div id='menu'>
					<ul>
						<li class='current_page_item'><a href='index.php'>Hem</a></li>
						<li><a href='testall.php'>Tester</a></li>
						<li><a href='#om'>Om</a></li>
					</ul>
					</div>
				</div>
			</div>
		<!-- end #header -->
		<div id='page'>
			<div id='page-bgtop'>
				<div id='page-bgbtm'>
					<div id='content'>
						$body
						<div style='clear: both;'>&nbsp;</div>
					</div>
					<!-- end #content -->
					<div id='sidebar'>
						<ul>
							<li>
								<div id='search' >
									<form method='get' action='#'>
										<div>
											<input type='text' name='s' id='search-text' placeholder='Sök..' value='' />
											<input type='submit' id='search-submit' value='GO' />
										</div>
									</form>
								</div>
								<div style='clear: both;'>&nbsp;</div>
							</li>
							<div id='om'></div>
							<li>
								<h2>Om</h2>
								<p>Detta är mitt PHP projekt</p>
								
							</li>
							<li>
								<h2>Blogroll</h2>
								<ul>
									$archive
								</ul>
							</li>
							<li>
								$meta
								$loginform
								
							</li>
						</ul>
					</div>
					<!-- end #sidebar -->
					<div style='clear: both;'>&nbsp;</div>
				</div>
			</div>
		</div>
		<!-- end #page -->
		<div id='footer'>
			<p>Copyright (c) 2012 edport.se. All rights reserved.</p>
		</div>
			</div>
			<!-- end #footer -->
			</body>
        </html>";

		return $xhtml;
	}

}
